package books.ThinkingInJava.src.Chapter18;

import books.ThinkingInJava.src.Tools.FileKit;
import books.ThinkingInJava.src.Tools.Output;

import java.io.*;

/**
 * @author bloodcolding
 * @since 12/27/2018 5:51 PM
 */
public class BufferedOutputFile {
    public static void main(String[] args) throws IOException {
        new BufferedOutputFile().outputFile("D:\\Doc\\Test", "a.txt", BufferedInputFile.class.toGenericString());
    }

    public boolean outputFile(String filePath, String fileName, String... strings) throws IOException {
        if (filePath == null) {
            return false;
        }
        String path = FileKit.filePathTransfer(filePath);
        File file = new File(path);
        file.mkdirs();
        path += "/" + fileName;
//        file = new File(path);
//        file.createNewFile();
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(path));
        for (String str : strings) {
//            bufferedWriter.write(str);
            bufferedWriter.newLine();
            bufferedWriter.append(str);
        }
        bufferedWriter.flush();
        bufferedWriter.close();
        LineNumberReader lineNumberReader = new LineNumberReader(new FileReader(path));
        String s = null;
        int i = 0;
        while ((s = lineNumberReader.readLine()) != null) {
            Output.println(lineNumberReader.getLineNumber() + s);
        }
        return true;
    }
}

